package com.tsvetanv.simpleformcontroller.example01.person.mvc.model;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Person {
	private Name name = new Name();
	private Date bornOn;
	private ProgrammingLanguage programmingLanguage;

	// --- Constructors, Getters/Setters ---------------------------------------

	public Person() {
	}

	public Person(Name name, Date bornOn,
			ProgrammingLanguage programmingLanguage) {
		this.name = name;
		this.bornOn = bornOn;
		this.programmingLanguage = programmingLanguage;
	}

	public Name getName() {
		return name;
	}

	public void setName(Name name) {
		this.name = name;
	}

	public Date getBornOn() {
		return bornOn;
	}

	public void setBornOn(Date bornOn) {
		this.bornOn = bornOn;
	}

	public ProgrammingLanguage getProgrammingLanguage() {
		return programmingLanguage;
	}

	public void setProgrammingLanguage(ProgrammingLanguage programmingLanguage) {
		this.programmingLanguage = programmingLanguage;
	}

	// ---- Auxiliary methods --------------------------------------------------
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(30);
		// TODO extract the separator in an external property file
		char separator = ';';
		builder.append(getName().toString()).append(separator);

		// TODO extract the Date format string in an external property file
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

		builder.append(dateFormat.format(bornOn)).append(separator);
		builder.append(getProgrammingLanguage().getName());
		return builder.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Person)) {
			throw new IllegalArgumentException(
					"Illegal input parameter for equality testing.");
		}
		Person personObj = (Person) obj;
		boolean isEqualName = name.equals(personObj.getName());
		boolean isEqualBornOn = bornOn.equals(personObj.getBornOn());
		boolean isEqualLanguage = programmingLanguage.equals(personObj
				.getProgrammingLanguage());

		return (isEqualName && isEqualBornOn && isEqualLanguage);
	}
}
